package com.jorado.redis;

import java.util.Map;

/**
 * Redis操作类, 对于返回值是数值包装类型(包括Boolean)的情况, 返回默认值
 */
public class RedisSafeClient extends RedisClient {

    /**
     * redis处理类的构造函数
     *
     * @param redisClusters redis连接字符串
     */
    public RedisSafeClient(String redisClusters) {
        super(redisClusters);
    }

    /**
     * redis处理类的构造函数
     *
     * @param redisClusters redis连接字符串
     * @param fuseTime      熔断时间(毫秒为单位)
     */
    public RedisSafeClient(String redisClusters, long fuseTime) {
        super(redisClusters, fuseTime);
    }

    /**
     * redis处理类的构造函数
     *
     * @param redisClusters   redis连接字符串
     * @param connectTimeout  连接超时时间
     * @param socketTimeout   读取超时的时间
     * @param maxRedirections 最大重试次数
     */
    public RedisSafeClient(String redisClusters, int connectTimeout, int socketTimeout, int maxRedirections) {
        super(redisClusters, connectTimeout, socketTimeout, maxRedirections);
    }

    /**
     * redis处理类的构造函数
     *
     * @param redisClusters   redis连接字符串
     * @param connectTimeout  连接超时时间
     * @param socketTimeout   读取超时的时间
     * @param maxRedirections 最大重试次数
     * @param fuseTime        熔断时间(毫秒为单位)
     */
    public RedisSafeClient(String redisClusters, int connectTimeout, int socketTimeout, int maxRedirections,
                           long fuseTime) {
        super(redisClusters, connectTimeout, socketTimeout, maxRedirections, fuseTime);
    }

    /**
     * redis处理类的构造函数
     *
     * @param redisClusters   redis连接字符串
     * @param connectTimeout  连接超时时间
     * @param socketTimeout   读取超时的时间
     * @param maxRedirections 最大重试次数
     * @param fuseTime        熔断时间(毫秒为单位)
     */
    public RedisSafeClient(String redisClusters, int connectTimeout, int socketTimeout, int maxRedirections,
                           int maxTotal, int maxIdle, int minIdle, int maxWaitMillis, long fuseTime) {
        super(redisClusters, connectTimeout, socketTimeout, maxRedirections, maxTotal, maxIdle, minIdle, maxWaitMillis,
                fuseTime);
    }

    /**
     * redis处理类的构造函数
     *
     * @param redisClusters   redis连接字符串
     * @param connectTimeout  连接超时时间
     * @param socketTimeout   读取超时的时间
     * @param maxRedirections 最大重试次数
     * @param maxTotal        连接池配置参数(最大的连接数)
     * @param maxIdle         连接池配置参数(最大空闲连接)
     * @param minIdle         连接池配置参数(最少空闲连接)
     * @param maxWaitMillis   获取连接池中连接的最大等待时间
     */
    public RedisSafeClient(String redisClusters, int connectTimeout, int socketTimeout, int maxRedirections,
                           int maxTotal, int maxIdle, int minIdle, int maxWaitMillis) {
        super(redisClusters, connectTimeout, socketTimeout, maxRedirections, maxTotal, maxIdle, minIdle, maxWaitMillis);
    }

    /**
     * 执行Redis的DEL命令
     *
     * @param key
     * @return
     */
    public Long del(String key) {
        Long result = super.del(key);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的EXISTS命令
     *
     * @param key
     * @return
     */
    public Boolean exists(String key) {
        Boolean result = super.exists(key);
        return result == null ? Boolean.valueOf(false) : result;
    }

    /**
     * 执行Redis的EXPIRE命令
     *
     * @param key
     * @param seconds
     * @return
     */
    public Long expire(final String key, final int seconds) {
        Long result = super.expire(key, seconds);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的EXPIREAT命令
     *
     * @param key
     * @param unixTime
     * @return
     */
    public Long expireAt(final String key, final long unixTime) {
        Long result = super.expireAt(key, unixTime);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的PERSIST命令
     *
     * @param key
     * @return
     */
    public Long persist(final String key) {
        Long result = super.persist(key);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的PEXPIRE命令
     *
     * @param key
     * @param milliseconds
     * @return
     */
    public Long pexpire(final String key, final long milliseconds) {
        Long result = super.pexpire(key, milliseconds);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的PEXPIREAT命令
     *
     * @param key
     * @param millisecondsTimestamp
     * @return
     */
    public Long pexpireAt(final String key, final long millisecondsTimestamp) {
        Long result = super.pexpireAt(key, millisecondsTimestamp);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的TTL命令
     *
     * @param key
     * @return
     */
    public Long ttl(final String key) {
        Long result = super.ttl(key);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的INCR命令
     *
     * @param key
     * @return
     */
    public Long incr(final String key) {
        Long result = super.incr(key);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的INCRBY命令
     *
     * @param key
     * @param integer
     * @return
     */
    public Long incrBy(final String key, final long integer) {
        Long result = super.incrBy(key, integer);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的SETNX命令
     *
     * @param key
     * @param value
     * @return
     */
    public Long setnx(final String key, final String value) {
        Long result = super.setnx(key, value);
        return result == null ? Long.valueOf(0) : result;
    }


    /**
     * 执行Redis的DECR命令
     *
     * @param key
     * @return
     */
    public Long decr(final String key) {
        Long result = super.decr(key);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的DECRBY命令
     *
     * @param key
     * @param integer
     * @return
     */
    public Long decrBy(final String key, final long integer) {
        Long result = super.decrBy(key, integer);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的HDEL命令
     *
     * @param key
     * @param field
     * @return
     */
    public Long hdel(final String key, final String... field) {
        Long result = super.hdel(key, field);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的HEXISTS命令
     *
     * @param key
     * @param field
     * @return
     */
    public Boolean hexists(final String key, final String field) {
        Boolean result = super.hexists(key, field);
        return result == null ? Boolean.valueOf(false) : result;
    }

    /**
     * 执行Redis的HINCRBY命令
     *
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hincrBy(final String key, final String field, final long value) {
        Long result = super.hincrBy(key, field, value);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的HLEN命令
     *
     * @param key
     * @return
     */
    public Long hlen(final String key) {
        Long result = super.hlen(key);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的HSET命令
     *
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hset(final String key, final String field, final String value) {
        Long result = super.hset(key, field, value);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的HSETNX命令
     *
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hsetnx(final String key, final String field, final String value) {
        Long result = super.hsetnx(key, field, value);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的LINSERT命令,where=before
     *
     * @param key
     * @param pivot
     * @param value
     * @return
     */
    public Long lbeforeinsert(final String key, final String pivot, final String value) {
        Long result = super.lbeforeinsert(key, pivot, value);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的LINSERT命令,where=after
     *
     * @param key
     * @param pivot
     * @param value
     * @return
     */
    public Long lafterinsert(final String key, final String pivot, final String value) {
        Long result = super.lafterinsert(key, pivot, value);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的LLEN命令
     *
     * @param key
     * @return
     */
    public Long llen(final String key) {
        Long result = super.llen(key);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的LPUSH命令
     *
     * @param key
     * @param string
     * @return
     */
    public Long lpush(final String key, final String... string) {
        Long result = super.lpush(key, string);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的LPUSHX命令
     *
     * @param key
     * @param string
     * @return
     */
    public Long lpushx(final String key, final String... string) {
        Long result = super.lpushx(key, string);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的LREM命令
     *
     * @param key
     * @param count
     * @param value
     * @return
     */
    public Long lrem(final String key, final long count, final String value) {
        Long result = super.lrem(key, count, value);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的RPUSH命令
     *
     * @param key
     * @param string
     * @return
     */
    public Long rpush(final String key, final String... string) {
        Long result = super.rpush(key, string);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的RPUSHX命令
     *
     * @param key
     * @param string
     * @return
     */
    public Long rpushx(final String key, final String... string) {
        Long result = super.rpushx(key, string);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的SADD命令
     *
     * @param key
     * @param member
     * @return
     */
    public Long sadd(final String key, final String... member) {
        Long result = super.sadd(key, member);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的SCARD命令
     *
     * @param key
     * @return
     */
    public Long scard(final String key) {
        Long result = super.scard(key);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的SISMEMBER命令
     *
     * @param key
     * @param member
     * @return
     */
    public Boolean sismember(final String key, final String member) {
        Boolean result = super.sismember(key, member);
        return result == null ? Boolean.valueOf(false) : result;
    }

    /**
     * 执行Redis的SREM命令
     *
     * @param key
     * @param member
     * @return
     */
    public Long srem(final String key, final String... member) {
        Long result = super.srem(key, member);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZADD命令
     *
     * @param key
     * @param member
     * @return
     */
    public Long zadd(final String key, final Map<String, Double> scoreMembers) {
        Long result = super.zadd(key, scoreMembers);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZADD命令
     *
     * @param key
     * @param member
     * @return
     */
    public Long zadd(final String key, final double score, final String member) {
        Long result = super.zadd(key, score, member);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZCARD命令
     *
     * @param key
     * @param member
     * @return
     */
    public Long zcard(final String key) {
        Long result = super.zcard(key);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZCOUNT命令
     *
     * @param key
     * @param member
     * @return
     */
    public Long zcount(final String key, final String min, final String max) {
        Long result = super.zcount(key, min, max);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZCOUNT命令
     *
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Long zcount(final String key, final double min, final double max) {
        Long result = super.zcount(key, min, max);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZINCRBY命令
     *
     * @param key
     * @param score
     * @param member
     * @return
     */
    public Double zincrby(final String key, final double score, final String member) {
        Double result = super.zincrby(key, score, member);
        return result == null ? Double.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZRANK命令, 如果出错，返回-1
     *
     * @param key
     * @param member
     * @return
     */
    public Long zrank(final String key, final String member) {
        Long result = super.zrank(key, member);
        return result == null ? Long.valueOf(-1) : result;
    }

    /**
     * 执行Redis的ZREM命令
     *
     * @param key
     * @param member
     * @return
     */
    public Long zrem(final String key, final String... member) {
        Long result = super.zrem(key, member);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZREMRANGEBYRANK命令
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Long zremrangeByRank(final String key, final long start, final long end) {
        Long result = super.zremrangeByRank(key, start, end);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZREMRANGEBYSCORE命令
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Long zremrangeByScore(final String key, final double start, final double end) {
        Long result = super.zremrangeByScore(key, start, end);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZREVRANK命令
     *
     * @param key
     * @param member
     * @return
     */
    public Long zrevrank(final String key, final String member) {
        Long result = super.zrevrank(key, member);
        return result == null ? Long.valueOf(0) : result;
    }

    /**
     * 执行Redis的ZSCORE命令
     *
     * @param key
     * @param member
     * @return
     */
    public Double zscore(final String key, final String member) {
        Double result = super.zscore(key, member);
        return result == null ? Double.valueOf(0) : result;
    }

}
